package com.play.ballen.pack.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.mall.entity.MallBallLevel
import com.play.ballen.me.entity.WithDrawInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.play.ballen.pack.entity.PackBallInfo
import com.play.ballen.pack.entity.PackBallSellInfo
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

open class PackVM : BaseViewModel() {

    protected val model = ApiRepository()

    var page = 1
    private val pageSize = 20

    val listBall = MutableLiveData<List<PackBallInfo>?>()
    val listMallBallLevel = MutableLiveData<List<MallBallLevel>?>()
    val totalDestroy = MutableLiveData<String>()
    val actionGetBallFailure = SingleLiveEvent<String>()

    val ballSellInfo = SingleLiveEvent<PackBallSellInfo>()

    val actionSaleSuccess = SingleLiveEvent<Void>()
    val actionSaleCancelSuccess = SingleLiveEvent<Void>()

    fun memberBasketBall(status: String, levelId: String? = "") {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize
        map["status"] = status
        if (!levelId.isNullOrBlank()) {
            map["level_id"] = levelId
        }

        model.memberBasketBall(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({ data ->
                listBall.value = data.list
                listMallBallLevel.value = data.level
                totalDestroy.value = data.total_destroy
                Logger.i("PackVM.memberBasketBall success list = $data")
            }, { throwable ->
                actionGetBallFailure.value = throwable.message
                Logger.i("PackVM.memberBasketBall error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun basketballSell(id: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        model.basketballSell(id)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("PackVM.basketballSell success")
                ballSellInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("PackVM.basketballSell error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun basketballSellConfirm(id: String, price: String, num: Int) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["id"] = id
        map["price"] = price
        map["num"] = num
        model.basketballSellConfirm(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("PackVM.basketballSellConfirm success")
                actionSaleSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("PackVM.basketballSellConfirm error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun basketballSellCancel(id: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["id"] = id
        model.basketballSellCancel(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("PackVM.basketballSellCancel success")
                toastLiveData.value = ResourceUtil.getString(R.string.pack_sale_cancel_success)
                actionSaleCancelSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("PackVM.basketballSellCancel error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

}